
/**
* dataService - AngularJS module for web api management.
*
*
* Credits
* =======
*
* Copyright 2016 LonghronShen <LonghronShen@Hotmail.com>
*/
class IStrackService{

    constructor(private http: ng.IHttpService, private $q:any, private routeService:routeService){
            //var url = this.routeService.Entity.History;
        }

    /**
     * 需要注意的是 jquery的ajax 请求需要在web server环境下才能获取数据 要不然会出现XMLHttpRequest cannot load 错误
     * 获取鹰眼服务数据 需要开发者自己实现后台服务(php java都可以只要能提供webservice服务 和web demo前端文件放在一个域名目录下(解决ajax跨域问题)，后台服务负责请求鹰眼服务
     * 前端js负责与后台服务交互数据 p.s:需要ak参数
     */
    get=(url, params, success, before, fail, after)=> {
        if (before) {
            before();
        }
        fail = fail || function () {
        };
        after = after || function () {
        };
        $.ajax({
            url: url,
            data: params,
            dataType: 'json',
            success: success,
            error: fail,
            complete: after
        });
    }

    /**
     * 添加jsonp接口，实现跨域请求数据
     */
    jsonp=( url, params, success, before, fail, after)=> {
        var deferred = this.$q.defer();

        if (before) {
            before();
        }

        fail = fail || function () {
            deferred.reject("fail");
        };
        after = after || function () { };
        //function callback_fn(data) {
        //    console.log("callback_fn");
        //    console.log(data);
        //}
        $.ajax({
            url: url,
            data: params,
            jsonpCallback: "callback_fn",
            dataType: 'jsonp',
            success:  (data)=> {
                //console.log(data.entities);
                deferred.resolve(data);
                return data;
                //success: function (json) {
                //    return json.entities;
            },
            error: fail,
            complete: after
        });

        return deferred.promise;
    }
}



angular.module('trackService',[]).service("trackService",['$http', '$q', 'routeService',IStrackService])
